<?
    include "../../common.php" ;
    $opt = $_POST['opt'] ;
    $arr = [] ;
    if( $opt == 'getList' ){
        $page = $_POST['page'] ; 
        $perPage = $_POST['perPage'] ;
        $pageSt = ( $page - 1 ) * $perPage ; 
        $sql = "SELECT t.*,pt.typeName FROM `transimg` AS t JOIN ptype as pt ON t.typeId = pt.id WHERE is_delete = 1 ORDER BY t.id ASC LIMIT {$pageSt},{$perPage}" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
    }
    if( $opt == 'getPage' ){
        $perPage = $_POST['perPage'] ;
        $sql = "SELECT COUNT(t.id) AS c FROM `transimg` AS t JOIN ptype as pt ON t.typeId = pt.id WHERE is_delete = 1 ORDER BY t.id ASC" ; 
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $pageNum = ceil( $one['c'] / $perPage );
            $arr['totalPage'] = $pageNum ; 
        }
    }
    if( $opt == 'addTransImg' ){
        $title = $_POST['title'] ;
        $typeName = $_POST['typeName'] ;
        $now =Time();
        $str = date('Ymd',Time()) ;
        if(!file_exists("../../../uploads/{$str}")){     //看是否存在文件夹uploads
            mkdir("../../../uploads/{$str}");
        }
        $path = $_FILES['srcImg']['tmp_name'];
        move_uploaded_file($path,"../../../uploads/{$str}/"."{$now}.jpg");
        $sql = "INSERT INTO `transimg`(`imgSrc`, `title`, `typeId`) VALUES ('{$str}/{$now}.jpg','{$title}',{$typeName})" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ; 
            $arr['info'] = '添加成功!' ;
        } else {
            $arr['status'] = 0 ; 
            $arr['info'] = '操作失败!' ;
        }
    }
    if( $opt == 'delTransImg' ){
        $myid = $_POST['myid'] ;
        $sql = "UPDATE `transimg` SET is_delete = 2 WHERE id = {$myid}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ; 
            $arr['info'] = '删除成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ;
        }
    }
    echo json_encode($arr);
?>